<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">小胖</div>
    <script>
        function isObject(value){
            return typeof value === 'object' && value !== null
        }
        function reactive(data){
            if(!isObject(data)){
                return data
            }
            return new Proxy(data,{
               get(target,key){
                    return target[key]
               },
               set(target,key,value){
                    target[key] = value
                    effect()//调用副作用函数，调用effect函数会重新获取新的数据
                    return true
               }
            })
        }
        //定义一个响应式数据:'触发者'
        const state=reactive({name:'小胖'})
        //定义一个响应式数据:'响应者'
        function effect(){
            //console.log(state.name)
            app.innerHTML=state.name
        }
        //当state.name改变时，重新执行对应副作用函数effect
        setTimeout(()=>{
            state.name='李四'
        },2000)
        
    </script>
</body>
</html>